package cn.lzit.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

@WebServlet(name = "CartServlet", urlPatterns = "/cart")

public class CartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.print("<p>这里是结账处，您的姓名以及选择的商品是：<br>");
        HttpSession session = request.getSession();
        String personName = (String)session.getAttribute("customerName");
        out.println("<br>您的姓名："+personName);
        Enumeration enumGoods = session.getAttributeNames();
        out.println("<br>购物车中的商品：<br>");
        while (enumGoods.hasMoreElements()){
            String key = (String)enumGoods.nextElement();
            String goods = (String)session.getAttribute(key);
            if(!(goods.equals(personName))){
                out.println(key+"<br>");
            }
        }
        out.println("<p>点击超链接，链接到food.html的页面，购买食品。");
        out.println("<a href='food.html'>欢迎去food.html！</a>");
        out.println("<p>点击超链接，链接到login.html的页面，去修改姓名。");
        out.println("<a href='login.html'>欢迎去login.html！</a>");
        out.println("<p>点击超链接，链接到logout页面，退出。");
        out.println("<a href='/logout'>退出！</a>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
